package com.burgeon.backend.SHOPBACKEND.controller;

import com.burgeon.backend.SHOPBACKEND.entity.Order;
import com.burgeon.backend.SHOPBACKEND.entity.OrderDetails;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVO.OrderEntityItem;
import com.burgeon.backend.SHOPBACKEND.entity.OrderVo;
import com.burgeon.backend.SHOPBACKEND.service.BackendOrderService;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("shopbackent/order")
public class BackentOrderController extends BaseController {
    @Autowired
    private BackendOrderService orderService;


    @RequestMapping("information")
    public ResponseResult<HashMap> OrderInformation(String account) {
        HashMap data = orderService.OrderInformation(account);
        return new ResponseResult<HashMap>(SUCCESS,data);
    }


    @RequestMapping("upOrderRemarkData")
    public ResponseResult<Void> upOrderRemarkData(String id, String remark) {
        orderService.upOrderRemarkData(id, remark);
        return new ResponseResult<>(SUCCESS);
    }

    @RequestMapping("findOrderItem")
    public ResponseResult<OrderEntityItem> findOrderItem(String id, String account) {
        OrderEntityItem data = orderService.findOrderItem(id, account);
        return new ResponseResult<OrderEntityItem>(SUCCESS, data);
    }

    /**
     * 订单分类
     *
     * @param start
     * @param state   0-未支付，1-已支付，2-已发货 3-待评论 4 已完成
     * @param account
     * @return
     */
    @RequestMapping("findOrderData")
    public ResponseResult<HashMap> findOrderData(Integer start, String state, String account) {
        HashMap data = orderService.GetOrderData(account, state, start);
        return new ResponseResult<HashMap>(SUCCESS, data);
    }


    //******************************

    @RequestMapping("queryOrder") //xx
    public ResponseResult<List<OrderVo>> QueryOrder(Integer start, String status, String disStatus, String account) {

        List<OrderVo> data = new ArrayList<>();
        List<Order> orderList = orderService.QueryOrder(start, status, disStatus, account);

        for (Order order : orderList) {
            OrderVo orderVo = new OrderVo();

            orderVo.setId(order.getId());
            orderVo.setDocno(order.getDocno());
            orderVo.setBilldate(order.getBilldate());
            orderVo.setXsno(order.getXsno());
            orderVo.setYcsotype(order.getYcsotype());
            orderVo.setDescription(order.getDescription());
            orderVo.setReceiverName(order.getReceiverName());
            orderVo.setReceiverMobile(order.getReceiverMobile());
            orderVo.setReceiverAddress(order.getReceiverAddress());
            orderVo.setAddressDesc(order.getAddressDesc());
            orderVo.setStatus(order.getStatus());
            orderVo.setDisStatus(order.getDisStatus());
            orderVo.setIsUp(order.getIsUp());
            orderVo.setTotLines(order.getTotLines());
            orderVo.setTotQty(order.getTotQty());
            orderVo.setTotAmtList(order.getTotAmtList());
            orderVo.setTotAmtActual(order.getTotAmtActual());
            //查询详情
            List<OrderDetails> orderDetailsList = orderService.QueryOrderDetails(order.getId(), account);
            orderVo.setOrderDetailsList(orderDetailsList);
            data.add(orderVo);
        }
        return new ResponseResult<List<OrderVo>>(SUCCESS, data);
    }


//    @RequestMapping("queryOrderbydocno")
//    public ResponseResult<List<Order>> QuerySOrderRetailsCode(String docno,String state, HttpSession session) {
//        //String account = session.getAttribute("account").toString();
//        String account = "321";//test
//        List<Order> data = new ArrayList<>();
//        List<Order> orderList = orderService.QuerySOrderRetailsCode(docno,account,state);
//        //循环遍历，放入返回数组中
//        for (Order order : orderList) {
//            Order sorder = new Order();
//
//            sorder.setId(order.getId());
//            sorder.setBilldate(order.getBilldate());
//            sorder.setXsno(order.getXsno());
//
//            sorder.setStatus(order.getStatus());
//            sorder.setDisStatus(order.getDisStatus());
//
//
//            data.add(sorder);
//        }
//        return new ResponseResult<List<Order>>(SUCCESS, data);
//    }

}